package com.admin.web.controller.work;

import com.admin.common.annotation.DataScope;
import com.admin.common.annotation.Log;
import com.admin.common.constant.Constants;
import com.admin.common.core.controller.BaseController;
import com.admin.common.core.domain.AjaxResult;
import com.admin.common.core.page.TableDataInfo;
import com.admin.common.enums.BusinessType;
import com.admin.common.utils.http.HttpUtils;
import com.admin.work.domain.WorkBuyInfo;
import com.admin.work.service.IWorkBuyInfoService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购买信息操作处理
 *
 * @author fzgnin
 */
@RestController
@RequestMapping("/work/buyinfo")
public class WorkBuyInfoController extends BaseController {
    @Autowired
    private IWorkBuyInfoService buyInfoService;
    /**
     * 获取购买信息列表
     */
    @PreAuthorize("@ss.hasPermi('work:buyinfo:list')")
    @GetMapping("/list")
    @DataScope(deptAlias = "buy")
    public TableDataInfo list(WorkBuyInfo buyinfo) {
        startPage();
        List<WorkBuyInfo> list = buyInfoService.selectBuyInfoList(buyinfo);
        list.forEach(item -> {
            item.setOwn(getUserId());

            try {
                String rspStr = HttpUtils.sendGet("http://api.mairui.club/hsrl/ssjy/" + item.getCode() + "/6aa14051a56a166357");
                JSONObject obj = JSONObject.parseObject(rspStr);
                Double p = Double.valueOf(obj.getFloat("p"));

                String pStr = String.format("%.2f", obj.getFloat("p"));

                item.setAmount();
                item.setZxj();
                item.setXj(Double.parseDouble(pStr));
                item.setSy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        return getDataTable(list);
    }

    /**
     * 根据购买信息编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('work:buyinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(buyInfoService.selectBuyInfoByID(id));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('work:buyinfo:add')")
    @Log(title = "购买信息管理", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@Validated @RequestBody WorkBuyInfo buyInfo) {
        buyInfo.setUserID(getUserId());
        buyInfo.setDeptID(getDeptId());
        buyInfo.setAmount();
        return toAjax(buyInfoService.insertBuyInfo(buyInfo));
    }

    /**
     * 修改购买信息
     */
    @PreAuthorize("@ss.hasPermi('work:buyinfo:edit')")
    @Log(title = "购买信息管理", businessType = BusinessType.UPDATE)
    @PutMapping("edit")
    public AjaxResult edit(@Validated @RequestBody WorkBuyInfo buyInfo)
    {
        buyInfo.setUserID(getUserId());
        buyInfo.setDeptID(getDeptId());
        buyInfo.setAmount();
        return toAjax(buyInfoService.updateBuyInfo(buyInfo));
    }
}
